 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { MainStrategy } from "GameLoop/WorldState/EntityManager/CreateStrategy/MainStrategy";
import { getRoleConfig } from "utils/constants/bodyconstant";
import { priority } from "utils/constants/definetypes";
import { BodyUtils } from "utils/creep/bodyutils";
import { Roles } from "utils/creep/setups";
import { PositionHelper } from "utils/position/PositionHelper";
import { generateUniqueId } from "utils/utils";

/**
 * 元素矿的运输策略
 */
export class TransportMinerialDecision< T extends "creep"="creep"> extends ABaseDecision<T>
{

    public isExpired(event: NormalEvent, region: string): boolean 
    {

 
        const lordroom = Game.rooms[event.region] as Required<Room>;
        if(lordroom.controller.level<6)
        {
            return false;
        }
        if(!lordroom.canExecute(11))
        {
            return false;
        }
    
        return true;
    }
    public makeTask(event: IFillStoreEvent, region: string): IDecisionCode<T>|false
    {
        // global.Log.Info(`维修任务`);
        const lordroom = Game.rooms[event.region] as Required<Room>;
        
      
        const coreEntity = lordroom.getEntity("CoreEntity");
        const  Mine = lordroom.getEntitys("MinerialEntity");
        const stores = coreEntity.getStores({resourceType:RESOURCE_ENERGY,amount:10000});
        if(stores.length==0)
        {
            return false;
        }
        for(const e of Mine)
        {
            if(e.getNodeMemory().state)
            {
                lordroom.deleteTaskById(e.GUID(),"creep")
                continue;
            }
            const container = e.getContainer();
            if(!container || container.store.getFreeCapacity()>300)
            {
                // container没有 则跳过
                continue;
            }
            if(lordroom.getTaskById(`${e.GUID()}_transfer`,["creep"]))
            {
                continue;
            }
            const minerialType = e.getMinerial().mineralType;
            if( stores.length>0  )
            {
                const Task:ICreepTaskData<"transport"> = 
                {
                    type:"transport",
                    arg:{
                        From:PositionHelper.getObjectRef(container),
                        To:PositionHelper.getObjectRef(stores[0]),
                        ResourceType:minerialType,
                        amount:container.store.getUsedCapacity(minerialType)
                    },
                    creeps:[],
                    body:{},
                    income:-1,
                    room:e.roomName,
                    cost:50,
                    required:[
                        [['carry'], 1,1]
                    ],
                    // disableSpawn:true
                }
                const code:IDecisionCode<T>={
                    region:event.region,
                    isunique:false,
                    task:[{
                        id:`${e.GUID()}_transfer`,
                        ttl:5000,
                        priority:priority.C,
                        type:"creep",
                        data:Task,
                        shard:1,
                    }],
                }
                return code;
            }
        }

        return false;
    }
    
}